package com.tledu.production.controller.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tledu.production.constant.CommonConstant;
import com.tledu.production.constant.SessionConstant;
import com.tledu.production.core.result.AjaxResult;
import com.tledu.production.model.system.SysUser;
import com.tledu.production.service.system.ISysUserService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 用户管理
 *
 * @author Antg
 * @date 2021/8/15  23:40
 */
@RestController
@AllArgsConstructor
@RequestMapping("/system/user")
public class SysUserController {
    private final ISysUserService sysUserService;

    @GetMapping("/list")
    public AjaxResult<PageInfo<SysUser>> findAll(Integer pageNum, Integer pageSize) {
        if (pageNum == null) pageNum = 1;
        if (pageSize == null) pageSize = CommonConstant.PAGESIZE;
        PageHelper.startPage(pageNum, pageSize);
        List<SysUser> list = sysUserService.findAll();
        PageInfo<SysUser> page = PageInfo.of(list);
        return new AjaxResult<>(page);
    }

    @PostMapping("/add")
    public AjaxResult add(SysUser sysUser) {
        int res = sysUserService.add(sysUser);
        if (res == 1) {
            return new AjaxResult("添加成功");
        } else {
            return new AjaxResult("添加失败", 500);
        }
    }

    @PostMapping("/edit")
    public AjaxResult edit(SysUser sysUser) {
        int res = sysUserService.edit(sysUser);
        if (res == 1) {
            return new AjaxResult("编辑成功");
        } else {
            return new AjaxResult("编辑失败", 500);
        }
    }

    @GetMapping("/del")
    public AjaxResult batchDel(String ids, HttpSession session) {
        int res = sysUserService.batchDel(ids.split(","), (SysUser) session.getAttribute(SessionConstant.LOGINUSER));
        if (res >= 1) {
            return new AjaxResult("删除成功");
        } else {
            return new AjaxResult("删除失败", 500);
        }
    }

    @GetMapping("/search")
    public AjaxResult<PageInfo<SysUser>> search(String type, String searchText, Integer pageNum, Integer pageSize) {
        if (pageNum == null) pageNum = 1;
        if (pageSize == null) pageSize = CommonConstant.PAGESIZE;
        PageHelper.startPage(pageNum, pageSize);
        List<SysUser> list = sysUserService.search(type, searchText);
        PageInfo<SysUser> page = PageInfo.of(list);
        return new AjaxResult<>(page);
    }

    @GetMapping("/lock")
    public AjaxResult lock(@RequestParam("locked") Boolean locked, @RequestParam("id") String id, HttpSession session) {
        SysUser loginUser = (SysUser) session.getAttribute(SessionConstant.LOGINUSER);
        int res = 0;
        if (locked) {
            res = sysUserService.lock("1", id, loginUser);
        } else {
            res = sysUserService.lock("0", id, loginUser);
        }
        if (res == 1) {
            return new AjaxResult("成功");
        } else {
            return new AjaxResult("失败", 500);
        }

    }
}
